package ch.ethz.inf.shop;

import java.io.IOException;
import java.lang.reflect.Field;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ch.ethz.inf.shop.model.DatastoreInterface;
import ch.ethz.inf.shop.model.DatastoreInterfaceSimpleDatabase;
import ch.ethz.inf.shop.model.Recipe;
import ch.ethz.inf.shop.model.RecipeItem;
import ch.ethz.inf.shop.util.html.BeanTableHelper;

/**
 * Servlet implementation class of Product Details Page
 */
@WebServlet(description = "Displays a specific product.", urlPatterns = { "/Recipe" })
public final class RecipeServlet extends HttpServlet {

	private static final Logger LOGGER = Logger.getLogger(RecipeServlet.class);
	private static final long serialVersionUID = 1L;
	private static final String RECIPE_ID = "recipeId";
	private final DatastoreInterface dbInterface = new DatastoreInterfaceSimpleDatabase();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

		final HttpSession session = request.getSession(true);

		final String idString = request.getParameter(RECIPE_ID);
		if (idString == null) {
			this.getServletContext().getRequestDispatcher("/Recipes").forward(request, response);
		}

		try {

			final Integer id = Integer.parseInt(idString);

			final Recipe recipe = this.dbInterface.getRecipeById(id);

			final BeanTableHelper<Recipe> table = new BeanTableHelper<Recipe>("recipe", "productTable", Recipe.class);

			for (Field f : Recipe.class.getDeclaredFields()) {
				Title titleAnnotation = f.getAnnotation(Title.class);
				if (titleAnnotation != null && titleAnnotation.isVisible()) {
					table.addBeanColumn(titleAnnotation.text(), f.getName());
				} else if (titleAnnotation == null) {
					table.addBeanColumn(f.getName(), f.getName());
				}
			}

			table.addObject(recipe);
			table.setVertical(true);

			session.setAttribute("recipeTable", table);

			final BeanTableHelper<RecipeItem> recipeItemsTable = new BeanTableHelper<RecipeItem>("recipeItems", "productsTable", RecipeItem.class);

			recipeItemsTable.addBeanColumn("Product Name", "name");
			recipeItemsTable.addBeanColumn("Product Weight", "weight");
			recipeItemsTable.addBeanColumn("Product Country of Origin", "countryOfOrigin");
			recipeItemsTable.addBeanColumn("Product Category", "category");
			recipeItemsTable.addBeanColumn("Product Brand", "brand");
			recipeItemsTable.addBeanColumn("Amount", "amount");
			recipeItemsTable.addBeanColumn("Price", "linePrice");

			recipeItemsTable.addLinkColumn("", "<img src=\"images/document-open.png\" alt=\"View Product\"/>", "Product?productId=", "productId");

			recipeItemsTable.addObjects(dbInterface.getRecipeItemsByRecipe(recipe));

			recipeItemsTable.setVertical(false);

			session.setAttribute("recipeItemTable", recipeItemsTable);

		} catch (final Exception ex) {

			LOGGER.error("", ex);
			this.getServletContext().getRequestDispatcher("/Recipes").forward(request, response);

		}

		this.getServletContext().getRequestDispatcher("/Recipe.jsp").forward(request, response);

	}
}
